package ly;


public class AudioPlayer {
    static {
        System.loadLibrary("player_jni");
    }
    public class AudioPCMInfo {
        public int chanee;
    }
    public interface Listener {
        public void onStateChanged(AudioPlayer player);
        public void onSpectrum(AudioPlayer player, int[] spec, int N);
    }
    private Listener listener = null;
    public enum State {
        IDLE,
        LOADING,
        PLAYING,
        PAUSED,
    }
    public enum Error {
        DECODER,
    }
    private long nativePointer;
    
    private void init(String path) {
        nativePointer = nativeCtl(1, nativePointer, path, 0);
    }

    public AudioPlayer(String path) {
        init(path);
    }
    public Listener getListener() {
        return listener;
    }
    public void setListener(Listener listener) {
        this.listener = listener;
    }
    public State getState() {
        long code = nativeCtl(2, nativePointer, null, 0);
        switch ((int)code) {
            case 0: return State.IDLE;
            case 1: return State.LOADING;
            case 2: return State.PLAYING;
            case 3: return State.PAUSED;
        }
        return State.IDLE;
    }
    public void seekTo(int ms) {
        nativeCtl(3, nativePointer, null, ms);
    }
    public boolean load() {
        return 1 == nativeCtl(4, nativePointer, null, 0);
    }
    public boolean resume() {
        return 1 == nativeCtl(5, nativePointer, null, 0);
    }
    public void pause() {
        nativeCtl(6, nativePointer, null, 0);
    }
    public void stop() {
        nativeCtl(7, nativePointer, null, 0);
    }
    public void destroy() {
        nativeCtl(8, nativePointer, null, 0);
    }

    public Error getErrorCode() {
        long code = nativeCtl(9, nativePointer, null, 0);
        switch ((int)code) {
            case 0: return Error.DECODER;
        }
        return Error.DECODER;
    }
    public String getErrorString() {
        return _getErrorString(nativePointer);
    }
    private void __nativeListenerSend(int event) {
        if (listener != null) listener.onStateChanged(this);
    }
    private void __nativeListenerSendSpectrum(int[] spectrum, int N) {
        if (listener != null) listener.onSpectrum(this, spectrum, N);
    }
    private native String _getErrorString(long nativePointer);
    private native long nativeCtl(int cmd, long nativePointers, String p1, int p2);
}
